using System;
using System.IO;
using System.Collections.Generic;

namespace TeamTasker.Utils
{
    /// <summary>
    /// This class encapsulates file-related utility methods. 
    /// </summary>
    public class FileUtils
    {
        /// <summary>  
        ///<para>Verifies if a specified file exists.</para>  
        /// </summary>  
        /// <param name="fileName">filename, which existence should be checked</param>
        public static bool FileExists(string fileName)
        {
            try
            {
                FileInfo info = new FileInfo(fileName);
                return info.Exists;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>  
        ///<para>Writes line to specified file. If <paramref name="value"/> is null, this method clears a file</para>  
        /// </summary>  
        /// <param name="filePath">Name of the file to write string</param>
        /// <param name="value">Text to write to file</param>
        public static void WriteTextToFile(string filePath, string value)
        {
            StreamWriter stream = null;
            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    bool exists = FileExists(filePath);
                    stream = new StreamWriter(filePath, exists);
                    if (value != null)
                    {
                        stream.Write(value);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Failed to write text to file: " + filePath, ex);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        public static string ReadFileAsText(string filePath)
        {
            string result = null;
            StreamReader stream = null;
            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    stream = new StreamReader(filePath);
                    result = stream.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Log.Error("Failed to read text from file: " + filePath, ex);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return result;
        }

        public static string[] ReadFileAsStrings(string filePath)
        {
            string content = ReadFileAsText(filePath);
            string[] lines = content.Split('\n', '\r');
            List<string> result = new List<string>();
            foreach (string line in lines) 
            {
                if (StringUtils.IsNotBlank(line) && !line.StartsWith(";"))
                {
                    result.Add(line.Trim());
                }
            }
            return result.ToArray();
        }

    }
}
